<?php
/**
 * Setting Model
 *
 * This class deals with setting which we use
 * to controll whole application behaviour
 *
 * @copyright 
 * @version   $Id$
 */
class Settings extends Core_Model
{
	/**
	 * Get settings of given name
	 * @param string $name - returns the string containg the setting 
	 */
	public static function getSetting($name)
	{
		$settings = App::getModel("default", "settings")->_getAllSettings();
		return $settings[$name];
	}
	
	/**
	 * Gets all settings from database, 
	 * saves it to cache
	 */
	private function _getAllSettings()
	{
		$cacheId = 'appsettings_'.APPLICATION_ENV;
        $cache = App::getCache();
        if (!($settings = $cache->load($cacheId))) {
            $settings = App::getDAO("default", "settings")->getAllSettings();
            $settings = $this->_parseSettings($settings);
        	$cache->save($settings, $cacheId);
        }
        return $settings;
	}
	
	/**
	 * Parse Settings and reorder them in key value pairs
	 * @param Array $settingsArray - Array containing settings in key-value pair
	 */
	private function _parseSettings($settingsArray)
	{
		$settings = array();
		foreach( $settingsArray as $setting ) {
			$settings[$setting["setting_name"]] = $setting["value"];
		}	
		return $settings;
	}
	
	/**
	 * Add new settings, if setting already exists DAO checks that
	 * and update the existing one
	 * 
	 * @param string $name  - name of the setting
	 * @param string $value - value of the setting
	 */
	public static function addSetting($name, $value)
	{
		return App::getDAO("default", "settings")->addSetting($name, $value);
	}
}